/** @brief Define an abstracted key class and a data class whose instance is associated with a key in KangmoDB. 
 *
 *  key.h
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 24..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#ifndef _KD_KEY_H_ 
#define _KD_KEY_H_ (1)

#include "kdInfra.h"
#include "types.h"
#include "util.h"

/** @brief The key part of (KEY,DATA) pair stored in KangmoDB.
 */
class stgKey
{
private :
	stgValueLength length_;
	char * key_;
public :
	stgKey() : key_(NULL), length_( 0 )
	{
	};
	
	stgKey(const char * key) : key_((char*)key), length_( key!=NULL?strlen(key):0 )
	{
		KD_ASSERT(key != NULL);
	};
	
	stgKey(const char * key, const stgValueLength length) : key_((char*)key), length_((stgValueLength)length)
	{
		KD_ASSERT(key != NULL);
		KD_ASSERT(length > 0);
	};
	
	KD_INLINE const char * getKey() const
	{
		return key_;
	};

	KD_INLINE const stgValueLength getLength() const
	{
		return length_;
	};

	int compare(const stgKey & anotherKey) const
	{
		
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() );
	};
	
	/** @brief The == operator used in set implementations (ex> skip_cas.c ).
	 */
	KD_INLINE bool operator==(stgKey & anotherKey) const 
	{
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() ) == 0;
	}

	/** @brief The >= operator used in set implementations (ex> skip_cas.c ).
	 */
	KD_INLINE bool operator>=(stgKey & anotherKey) const 
	{
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() ) >= 0;
	}
	
	/** @brief The <= operator used in set implementations (ex> skip_cas.c ).
	 */
	KD_INLINE bool operator<=(stgKey & anotherKey) const 
	{
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() ) <= 0;
	}

	/** @brief The > operator used in set implementations (ex> skip_cas.c ).
	 */
	KD_INLINE bool operator>(stgKey & anotherKey) const 
	{
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() ) > 0;
	}
	
	/** @brief The < operator used in set implementations (ex> skip_cas.c ).
	 */
	KD_INLINE bool operator<(stgKey & anotherKey) const 
	{
		return util::string_compare(key_, length_, anotherKey.getKey(), anotherKey.getLength() ) < 0;
	}
};


#endif /* _KD_KEY_H_ */